#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "head.h"

int main(int argc, const char *argv[])
{
	printf("--------------Welcome to use MiniShell--------------\n");
	while(1)
	{
		//打印路径用户信息
		char path[256] = {0};
		getcwd(path,256);
		printf("linux@ubuntu:%s$ ",path);

		//命令处理
		char order[256] = {0};
		fgets(order,sizeof(order),stdin);
		order[strlen(order) - 1] = '\0';

		char *p[20] = {NULL};
		int i = 0;
		for(i = 0;i < 20;++i)
		{
			if(i == 0)
			{
				p[0] = strtok(order," ");
				if(p[i] == NULL)
				{
					break;
				}
			}
			else
			{
				p[i] = strtok(NULL," ");
				if(p[i] == NULL)
				{
					break;
				}
			}
		}

		if(strcmp(p[0],"quit") == 0)
		{
			printf("--------------Thanks For Using!--------------\n");
			break;
		}

		//执行命令
		if(strcmp(p[0],"cp") == 0)
		{
			Cp(p[1],p[2]);
		}
		else if(strcmp(p[0],"cat") == 0)
		{
			Cat(p[1]);
		}
		else if(strcmp(p[0],"cd") == 0)
		{
			Cd(p[1]);
		}
		else if(strcmp(p[0],"ls") == 0)
		{
			Ls(p[1]);
		}
		else if(strcmp(p[0],"ll") == 0)
		{
			Ll(p[1]);
		}
		else if(strcmp(p[0],"ln") == 0 && strcmp(p[1],"-s") == 0)
		{
			Symlink(p[2],p[3]);
		}
		else if(strcmp(p[0],"ln") == 0)
		{
			Hardlink(p[1],p[2]);
		}
		else if(strcmp(p[0],"rm") == 0)
		{
			Rm(p[1]);
		}
		else if(strcmp(p[0],"touch") == 0)
		{
			Touch(p[1]);
		}
		else if(strcmp(p[0],"mv") == 0)
		{
			Mv(p[1],p[2]);
		}
		else
		{
			printf("don't find cmd\n");
		}
	}
	return 0;
}
